﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using myComics.Model;
using myComics.BO;
using System.Threading;
using System.Diagnostics;

namespace myComics_Test
{
    /// <summary>
    /// Summary description for UnitTest1
    /// </summary>
    [TestClass]
    public class ComicsModel
    {
        public ComicsModel()
        {
            //
            // TODO: Add constructor logic here
            //
        }

        private TestContext testContextInstance;

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region Additional test attributes
        //
        // You can use the following additional attributes as you write your tests:
        //
        // Use ClassInitialize to run code before running the first test in the class
        // [ClassInitialize()]
        // public static void MyClassInitialize(TestContext testContext) { }
        //
        // Use ClassCleanup to run code after all tests in a class have run
        // [ClassCleanup()]
        // public static void MyClassCleanup() { }
        //
        // Use TestInitialize to run code before running each test 
        // [TestInitialize()]
        // public void MyTestInitialize() { }
        //
        // Use TestCleanup to run code after each test has run
        // [TestCleanup()]
        // public void MyTestCleanup() { }
        //
        #endregion

        [TestMethod]
        public void UrlVerification()
        {
            Comics comics = new Comics();
            List<Comic> comicsWithErrors = new List<Comic>();
            int i = 0;
            foreach (Comic comic in comics)
            {
                Debug.WriteLine(comic.Name);
                try
                {
                    AutoResetEvent autoResetEvent = new AutoResetEvent(false);
                    comic.PropertyChanged += (s, e) =>
                    {
                        if (e.PropertyName == "CurrentStrip")
                        {
                            if (comic.CurrentStrip == null)
                            {
                                comicsWithErrors.Add(comic);
                                //Debug.WriteLine(string.Format("Comic strip not found for {0}.", comic.Name));
                            }
                            autoResetEvent.Set();
                        }
                    };
                    comic.Refresh();
                    autoResetEvent.WaitOne();
                }
                catch (Exception ex)
                {
                    Assert.Fail(string.Format("Refresh error on {0}.", comic.Name));
                }
                
            }
            if (comicsWithErrors.Count > 0)
            {
                StringBuilder sb = new StringBuilder();
                foreach (Comic c in comicsWithErrors)
                    sb.Append(string.Format("{0} - {1}\r\n", c.Name, c.CurrentUrl));
                Assert.Fail("You have comics with errors :\r\n"+sb.ToString());                
            }
        }

        //[TestMethod]
        public void PreviousStrip()
        {
            Comics comics = new Comics();
            Comic calvinAndHobbes = comics[0];
            string lastStripUrl = string.Empty;
            AutoResetEvent autoResetEvent = new AutoResetEvent(false);
            calvinAndHobbes.PropertyChanged += (s, e) =>
                {
                    if (string.IsNullOrEmpty(lastStripUrl))
                    {
                        lastStripUrl = calvinAndHobbes.CurrentStrip.UriSource.AbsoluteUri;
                    }
                    else
                    {
                        Assert.AreNotEqual(lastStripUrl, calvinAndHobbes.CurrentStrip.UriSource.AbsoluteUri,"Failed to go to previous strip.");
                    }
                    autoResetEvent.Set();
                };
            calvinAndHobbes.Refresh();
            autoResetEvent.WaitOne();
            autoResetEvent.Reset();
            calvinAndHobbes.Previous();
            autoResetEvent.WaitOne();
        }

    }
}
